package com.ljkj.cordial.chat.presenter.response;

import android.app.Activity;

import com.lanjiu.pro.business.BusinessProtocolEntities;
import com.lanjiu.pro.business.BusinessProtocolMessages;
import com.ljkj.cordial.chat.base.ChatApp;
import com.ljkj.cordial.chat.dao.notify.SysNoticeTable;

import java.util.List;

public class SysNotifyProtocolResponse extends AResponseBusinessPresenter {
    @Override
    public void onResponse(Activity activity, boolean isOnScreen, String msgType, Object msg) {
        if (msg instanceof BusinessProtocolMessages.NotificationsProtocol) {
            BusinessProtocolMessages.NotificationsProtocol response = (BusinessProtocolMessages.NotificationsProtocol) msg;
            List<BusinessProtocolEntities.SysNotify> sysList = response.getSysNotifyList();
            if (sysList != null && !sysList.isEmpty()) {
                for (BusinessProtocolEntities.SysNotify sysNotifyDto : sysList) {
                    insert(sysNotifyDto);
                }
                if (isOnScreen) {
                    refresh(activity);
                }

            }
        }
        if (msg instanceof BusinessProtocolMessages.SysNotifyProtocol) {
            BusinessProtocolMessages.SysNotifyProtocol response = (BusinessProtocolMessages.SysNotifyProtocol) msg;
            if (response.hasSysNotify()) {
                BusinessProtocolEntities.SysNotify notice = response.getSysNotify();
                insert(notice);
                if (isOnScreen) {
                    refresh(activity);
                }
            }
        }
    }

    @Override
    public void insert(Object... table) {
        super.insert(table);
        try {
            BusinessProtocolEntities.SysNotify sysNotifyDto = (BusinessProtocolEntities.SysNotify) table[0];
            SysNoticeTable st = new SysNoticeTable();
            st.setUserId(ChatApp.getInstance().getUserId());
            st.setNotifyId(sysNotifyDto.getNotifyId());
            st.setAuthor(sysNotifyDto.getAuthor());
            st.setContent(sysNotifyDto.getContent());
            st.setTitle(sysNotifyDto.getTitle());
            st.setDate(sysNotifyDto.getDate());
            st.setUrl(sysNotifyDto.getUrl());
            if (sysNotifyDto.getPicture() != null) {
                st.setImgUrl(sysNotifyDto.getPicture().getUrl());
            }
            SysNoticeTable.insert(st);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
